home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 17 / examples / queens2.lsp < prev    next >
Encoding:
Lisp/Scheme  |  1988-07-13  |  2.2 KB  |  83 lines

  1. ;
  2. ; Place n queens on a board
  3. ;  See Winston and Horn Ch. 11
  4.  
  5. (defun cadar (x)
  6.   (car (cdr (car x))))
  7.  
  8. ; Do two queens threaten each other ?
  9. (defun threat (i j a b)
  10.   (or (equal i a)            ;Same row
  11.       (equal j b)            ;Same column
  12.       (equal (- i j) (- a b))        ;One diag.
  13.       (equal (+ i j) (+ a b))))        ;the other diagonal
  14.  
  15. ; Is poistion (n,m) on the board safe for a queen ?
  16. (defun conflict (n m board)
  17.   (cond ((null board) nil)
  18.     ((threat n m (caar board) (cadar board)) t)
  19.     (t (conflict n m (cdr board)))))
  20.  
  21.  
  22. ; Place queens on a board of size SIZE
  23. (defun queens (size)
  24.   (prog (n m board soln)
  25.     (setq soln 0)            ;Solution #
  26.     (setq board nil)
  27.     (setq n 1)            ;Try the first row
  28.     loop-n
  29.     (setq m 1)            ;Column 1
  30.     loop-m
  31.     (cond ((conflict n m board) (go un-do-m))) ;Check for conflict
  32.     (setq board (cons (list n m) board))       ; Add queen to board
  33.     (cond ((> (setq n (1+ n)) size)            ; Placed N queens ?
  34.            (print-board (reverse board) (setq soln (1+ soln))))) ; Print it
  35.     (go loop-n)                       ; Next row which column?
  36.     un-do-n
  37.     (cond ((null board) (return 'Done))        ; Tried all possibilities
  38.           (t (setq m (cadar board))           ; No, Undo last queen placed
  39.          (setq n (caar board))
  40.          (setq board (cdr board))))
  41.  
  42.     un-do-m
  43.     (cond ((> (setq m (1+ m)) size)          ; Go try next column
  44.            (go un-do-n))
  45.           (t (go loop-m)))))
  46.  
  47.  
  48. ;Print a board
  49. (defun print-board  (board soln &aux size)
  50.   (setq size (length board))        ;we can find our own size
  51.   (terpri)
  52.   (princ "\t\tSolution: ")
  53.   (print soln)
  54.   (terpri)
  55.   (princ "\t")
  56.   (print-header size 1)
  57.   (terpri)
  58.   (print-board-aux board size 1)
  59.   (terpri))
  60.  
  61. ; Put Column #'s on top
  62. (defun print-header (size n)
  63.   (cond ((> n size) terpri)
  64.     (t (princ n)
  65.        (princ " ")
  66.        (print-header size (1+ n)))))
  67.  
  68. (defun print-board-aux (board size row)
  69.   (terpri)
  70.   (cond ((null board))
  71.     (t (princ row)            ;print the row #
  72.        (princ "\t")
  73.        (print-board-row (cadar board) size 1) ;Print the row
  74.        (print-board-aux (cdr board) size (1+ row)))))  ;Next row
  75.  
  76. (defun print-board-row (column size n)
  77.   (cond ((> n size))
  78.     (t (cond ((equal column n) (princ "Q"))
  79.          (t (princ ".")))
  80.        (princ " ")
  81.        (print-board-row column size (1+ n)))))
  82.